package com.caifu.util;


import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Lyf
 * @Date: 2019/3/26 10:31
 * @Description:
 */
@Repository
public class SendingSms {

    public static Map<String, String> sendingSMS(String mobile,String random) throws Exception{
        Map<String, String> map = new HashMap<String, String>();
        try{
            //验证手机号格式是否正确
            if(mobile == null || mobile.length()!=11) {
                map.put("code", "9001");
                map.put("codeDesc", "手机号码有误");
                return map;
            }
            //String random = Utils2.getAppointlenRandom(4);   //4位手机验证码
            /**
             * 阿里短信发送通道
             */
            String host = "http://sms.market.alicloudapi.com";
            String path = "/singleSendSms";
            String method = "GET";
            String appcode = "2cda1cab44e74978b9556d2f777e4055";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            Map<String, String> querys = new HashMap<String, String>();

            querys.put("ParamString", "{'code':'"+random+"','product':'智慧社区'}");
            querys.put("RecNum", mobile);
            querys.put("SignName", "智慧社区");
            querys.put("TemplateCode", "SMS_69000245");

            try {
                HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
                //获取阿里返回值
                JSONObject json = JSONObject.fromObject(EntityUtils.toString(response.getEntity()));

                if(json.getString("success").equals("true")){
                    //短信发送成功，添加短信发送信息至短信发送记录表
//                        ShortMessage message = new ShortMessage();
//                        message.setPhone(mobile);
//                        message.setContent("【阿里通信】手机号:"+mobile+"短信验证码发送["+random+"],发送状态【"+json.toString()+"】");
//                        message.setCreateDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));
//                        message.setCreateTime(new SimpleDateFormat("HHmmss").format(new Date()));
//                        //userDao.editSave(message);
//                        teller.saveMessage(message);
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }

            map.put("code", "0000");
            map.put("codeDesc", "校验通过");
            map.put("mobile", DES.encryptDES(mobile));
            map.put("vertifyCode", DES.encryptDES(random));

            return map;

        }catch (Exception e){
            e.printStackTrace();
            map.put("code", "9999");
            map.put("codeDesc", "网络异常");
            return map;
        }

    }
}
